package cn.dw.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import cn.dw.db.DBO;
import cn.dw.model.Reader;

public class AddStudentWeb extends HttpServlet{


	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException
			{
		req.setCharacterEncoding("utf-8");
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String zhuanye = req.getParameter("zhuanye");
		Reader student = new Reader();
		student.setDzxm(id);
		student.setDzzh(name);
		System.out.println(student.toString());
		addStudent(student);

		req.setAttribute("msg", "添加成功！姓名："+name);
		req.getRequestDispatcher("/index.jsp").forward(req, resp);
			}

	public void addStudent(Reader student){

		CloseableHttpClient httpclient = HttpClients.createDefault();  

		HttpPost httpPost =new HttpPost("http://10.20.2.9/cgi-bin/HNModify.cgi");
		List<NameValuePair> params=new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("type", "4"));//操作类型新增人员 
		params.add(new BasicNameValuePair("id", ""));//此操作类型下无意义
		params.add(new BasicNameValuePair("eid", student.getDzzh()));//员工号
		params.add(new BasicNameValuePair("name", student.getDzxm()));//人员姓名
		params.add(new BasicNameValuePair("gender", "0"));//人员性别
		params.add(new BasicNameValuePair("parentId ", "3"));//人员所属部门ID
		params.add(new BasicNameValuePair("picture", ""));//照片
		params.add(new BasicNameValuePair("validity", "2099-10-10"));//有效期
		params.add(new BasicNameValuePair("synVersion", "0"));//固定为0
		System.out.println(student.getDzzh());
		System.out.println(student.getDzxm());

		try{
			System.out.println("开始执行");
			httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response = httpclient.execute(httpPost);
			String strResult= EntityUtils.toString(response.getEntity(),"utf-8");
		}catch (Exception e) {
			e.printStackTrace();
			System.out.println("error");
		}
		System.out.println("结束");

		try {
			CloseableHttpResponse response2 = httpclient.execute(httpPost);
			System.out.println(response2.getStatusLine()+"-------------");
			HttpEntity entity2 = response2.getEntity();
			EntityUtils.consume(entity2);
			response2.close();
		}catch (Exception e) {
			System.out.println("接受返回值失败！");
		}
	}

	public void updateStudent(Reader student){

		CloseableHttpClient httpclient = HttpClients.createDefault();  

		HttpPost httpPost =new HttpPost("http://10.20.2.9/cgi-bin/HNModify.cgi");
		List<NameValuePair> params=new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("type", "1"));//操作类型新增人员 
		params.add(new BasicNameValuePair("id", ""));//此操作类型下无意义
		params.add(new BasicNameValuePair("eid", student.getDzzh()));//员工号
		params.add(new BasicNameValuePair("name", student.getDzxm()));//人员姓名
		params.add(new BasicNameValuePair("gender", "0"));//人员性别
		params.add(new BasicNameValuePair("parentId ", "3"));//人员所属部门ID
		params.add(new BasicNameValuePair("picture", ""));//照片
		params.add(new BasicNameValuePair("validity", "2099-10-10"));//有效期
		params.add(new BasicNameValuePair("synVersion", "0"));//固定为0

		try{
			System.out.println("开始执行");
			httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
			HttpResponse response = httpclient.execute(httpPost);
			String strResult= EntityUtils.toString(response.getEntity(),"utf-8");
		}catch (Exception e) {
			e.printStackTrace();
			System.out.println("error");
		}
		System.out.println("结束");

		try {
			CloseableHttpResponse response2 = httpclient.execute(httpPost);
			System.out.println(response2.getStatusLine()+"-------------");
			HttpEntity entity2 = response2.getEntity();
			EntityUtils.consume(entity2);
			response2.close();
		}catch (Exception e) {
			System.out.println("接受返回值失败！");
		}
	}
}
